const baseURL = 'https://tapi.i-dock.net'; //请求服务器公共地址
// const baseURL = 'http://192.168.1.66:8001';//请求服务器公共地址
const http = (url, method, data, header) => {
	return new Promise((resolve, reject) => {
		try {

			let language = uni.getSystemInfoSync().language ||
				'';
			let lang = language.split('-')[0] || '';

			let networkErrorTitle = '';
			if (lang == 'zh') {
				networkErrorTitle = '当前网络不可用，请检查你的网络设置';
			} else if (lang == 'en') {
				networkErrorTitle = 'Network unavailable，please check';
			} else if (lang == 'ru') {
				networkErrorTitle = 'Сеть недоступна, проверьте';
			}

			//检查当前网络状态
			uni.getNetworkType({
				success: function(res) {
					// console.log("getNetworkType", res.networkType);

					if (res.networkType == 'none') {
						uni.hideLoading();
						if (networkErrorTitle != '') {
							uni.$u.toast(networkErrorTitle);
						}
						reject(res);
					}

					if (res.networkType != 'none') {

						//判断token是否存在或过期
						let newurl = ''
						const token = uni.getStorageSync('userInfo').token || ''
						const append = `token=${token}`;
						if (url.indexOf("?") > -1) {
							newurl = `${url}&${append}`;
						} else {
							newurl = `${url}?${append}`;
						}
						// console.log("请求拉地址拉", newurl)

						uni.request({
							url: newurl,
							method: method || 'POST', // 默认为POST请求 
							data: data, //请求超时在manifest.json配置
							header: header || {
								'content-type': 'application/x-www-form-urlencoded'
							},
							success: res => {
								let code = res.data.code;

								// console.log("---------"+JSON.stringify(lang));
								switch (code) {
									case 200:
										resolve(res.data.data);
										break;
									case 400000:
										break;
									case 400001:
										//如果接口抛出异常用户登录失效，则把当前页面的路由传递过去，验证成功后跳转回当前页面
										console.log()
										let pages = getCurrentPages();
										let page = pages[pages.length - 1];
										if (page.route && page.route !==
											'pages/login/index') {
											uni.$u.route({
												type: 'redirect',
												url: '/pages/login/index',
												params: {
													backUrl: encodeURIComponent(
														page.route + uni
														.$u
														.queryParams(
															page.options
														)), //登录失效页面路由
													redirectPage: 1, //是否为重定向页面
												},
												animationType: 'slide-in-bottom'
											});
										}
										break;
									default:
										break;
								}
								if (res.data.message) {
									if (res.data.message.indexOf('密码不正确') > -1) {
										if (lang == 'zh') {
											uni.$u.toast('密码不正确');
											// uni.$u.toast(vm.$i18n.t('mine').login_fail_ngpwd || '未知错误');
										} else if (lang == 'en') {
											uni.$u.toast('Incorrect Password');
										} else if (lang == 'ru') {
											uni.$u.toast(
												'Аккаунт или пароль неверен');
										}

									} else if (res.data.message.indexOf('账号不存在') > -
										1) {
										if (lang == 'zh') {
											uni.$u.toast('登录失败，该账号不存在');
											// uni.$u.toast(vm.$i18n.t('mine').login_fail_ngpwd || '未知错误');
										} else if (lang == 'en') {
											uni.$u.toast('Incorrect Username');
										} else if (lang == 'ru') {
											uni.$u.toast(
												'Аккаунт или пароль неверен');
										}
									} else {
										uni.$u.toast(res.data.message || '未知错误');
									}
								}
							},
							fail: (err) => {
								reject(err);
								console.log(err);
								uni.$u.toast(networkErrorTitle);

								/*错误码处理
								let code = err.data.code; 
								switch (code) {
								    case 500:
								        break;
								    default:
								        break;
								  } */
							},
							complete: () => {
								uni.hideLoading();
							}
						});
					}
				}
			});
		} catch (e) {
			uni.hideLoading();
			uni.showToast({
				title: networkErrorTitle,
				icon: 'none'
			});
		}

	});
};
const service = {};
//get方法封装
service.get = (url, data) => {
	return http(
		baseURL + url,
		'GET', // 默认为POST请求
		data, //请求超时在manifest.json配置
	);
};
//post方法封装
service.post = (url, data, header) => {
	return http(
		baseURL + url,
		'POST', // 默认为POST请求
		data, //请求超时在manifest.json配置
		header
	);
};
export default service;